﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Domain.Interface;
using Domain.Model;
using Ds.DataBase;
using Ds.DataBase.Model;
using Newtonsoft.Json;

namespace Domain.RealiZation
{
    public class LoginInfotmpl : ILoginInfo
    {
        public string GetUserPower(string name)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {

                var pt = db.PowerTreesContext.ToList();
                //var tt = from dd in db.PowerTreesContext select dd;
                //var strst = ((System.Data.Entity.Infrastructure.DbQuery<object>) query).Sql;
                //var zw = db.ZhiWeiContext.ToList();
                List<PowerTree> powertrees = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == name)?.keshi.keshizhiwus.SelectMany(s => s.PowerTrees).ToList();
                var userdata = db.JiaoZhiGongInfoContext.SingleOrDefault(s => s.Users.username == name);
                var ttt = new List<PowerTree>();
                if (userdata.kszw != null)
                {
                    ttt = userdata.kszw.PowerTrees.ToList();
                }
                List<Domain_PowerTree> lst = new List<Domain_PowerTree>();
                var parent_ptid = new List<string>();
                foreach (var item in ttt)
                {
                    parent_ptid.Add(item.ptpath.Split("/".ToCharArray())[1]);
                }
                HashSet<string> hs = new HashSet<string>(parent_ptid);
                foreach (var item in hs)
                {
                    int hsid = int.Parse(item);
                    var aaa = pt.SingleOrDefault(s => s.ptid == hsid);
                    var model = new Domain_PowerTree
                    {
                        mc = aaa.mc,
                        uri = aaa.uri
                    };
                    foreach (var item3 in ttt)
                    {
                        if (aaa.SonPowerTrees.Any(s => s.ptid == item3.ptid))
                        {
                            model.son.Add(new Domain_Son_PowerTree() { mc = item3.mc, uri = item3.uri });
                        }
                    }
                    lst.Add(model);
                }
                //foreach (var powertree in powertrees)
                //{
                //    foreach (var item in lst)
                //    {
                //        if (item.mc == powertree.mc)
                //        {
                //            foreach (var powertreeSonPowerTree in powertree.SonPowerTrees)
                //            {
                //                item.son.Add(new Domain_Son_PowerTree() { mc = powertreeSonPowerTree.mc, uri = powertreeSonPowerTree.uri });
                //            }
                //        }
                //    }
                //}
                return JsonConvert.SerializeObject(lst);
            }
        }

        public bool Signin(string username, string password)
        {
            using (UserInfoClassContext db = new UserInfoClassContext())
            {
                return db.UserInfoContext.Any(s => s.username == username && s.password == password);
            }
        }
    }
}
